/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pig.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.demo.entity.XlmOrder;
import com.pig4cloud.pig.demo.entity.XlmOrderverify;
import com.pig4cloud.pig.demo.entity.XlmUser;
import com.pig4cloud.pig.demo.service.XlmOrderService;
import com.pig4cloud.pig.demo.service.XlmOrderverifyService;
import com.pig4cloud.pig.demo.service.XlmUserService;
import com.pig4cloud.pig.demo.vo.QiNiu;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;


/**
 * 订单提交记录
 *
 * @author xiaoxiong
 * @date 2022-04-08 18:01:37
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ddtj")
@Api(value = "ddtj", tags = "订单提交记录管理")
public class XlmOrderverifyController {

    private final XlmOrderverifyService xlmOrderverifyService;

    private final XlmOrderService xlmOrderService;

    private final XlmUserService xlmUserService;

    @Value("${oss.endpoint:'https://upload.qiniup.com/'}")
    private String endpoint;

    @Value("${oss.accessKey:'awrA3sreOdT6aNRbMMuH9y06CPsqb4ASorG9-NfS'}")
    private String accessKey;

    @Value("${oss.secretKey:'jXYxs0_Putoxx6HvJzdpSAdHY0HyUSYQaBhfpD-f'}")
    private String secretKey;

    @Value("${oss.bucket-name:'xiangleg'}")
    private String bucketname;

    /**
     * 通过订单
     *
     * @return R
     */
    @ApiOperation(value = "通过订单", notes = "通过订单")
    @SysLog("通过订单")
    @PutMapping("/tg/{id}")
    public R tg(@PathVariable("id") Integer id) {
        XlmOrderverify one = xlmOrderverifyService.getById(id);
        if (one != null) {
            XlmOrder byId = xlmOrderService.getById(one.getOrderid());
            if (byId != null) {
                //修改订单状态
                byId.setStatus(2);
                xlmOrderService.saveOrUpdate(byId);
                //修改订单审核状态
                one.setStatus(2);
                xlmOrderverifyService.updateById(one);
                //增加余额
                XlmUser user = xlmUserService.getOne(Wrappers.<XlmUser>lambdaQuery().eq(XlmUser::getUserid, byId.getPayid()));
                user.setBalance(user.getBalance()+byId.getReturnmoney());
                xlmUserService.updateById(user);
                return R.ok("修改成功！");
            } else {
                return R.failed("没有关联的订单！");
            }
        } else {
            return R.failed("没有此提交记录！");
        }
    }


    /**
     * 驳回订单
     *
     * @return R
     */
    @ApiOperation(value = "驳回订单", notes = "驳回订单")
    @SysLog("驳回订单")
    @PutMapping("/bh/{id}")
    public R bh(@PathVariable("id") Integer id) {
        XlmOrderverify one = xlmOrderverifyService.getById(id);
        if (one != null) {
            XlmOrder byId = xlmOrderService.getById(one.getOrderid());
            if (byId != null) {
                //修改订单状态
                byId.setStatus(3);
                xlmOrderService.saveOrUpdate(byId);
                //修改订单审核状态
                one.setStatus(3);
                xlmOrderverifyService.updateById(one);
                return R.ok("修改成功！");
            } else {
                return R.failed("没有关联的订单！");
            }
        } else {
            return R.failed("没有此提交记录！");
        }
    }

    /**
     * 获取七牛的token
     *
     * @return R
     */
    @ApiOperation(value = "获取七牛的token", notes = "获取七牛的token")
    @SysLog("获取七牛的token")
    @GetMapping("/getToken")
    public R getToken() {
        QiNiu qiNiu = new QiNiu();

        long expireSeconds = 10000;   //过期时间
        StringMap putPolicy = new StringMap();
        Auth auth = Auth.create(accessKey, secretKey);
        String key = UUID.randomUUID().toString().replaceAll("\\-", "");
        String upToken = auth.uploadToken(bucketname, key);
        qiNiu.setKey(key);
        qiNiu.setToken(upToken);
        return R.ok(qiNiu);
    }

    /**
     * 提交订单
     *
     * @param orderverify
     */
    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param xlmOrderverify 订单提交记录
     * @return
     */
    @ApiOperation(value = "提交订单", notes = "提交订单")
    @PutMapping("/submitmessage")
    public R submitmessage(@RequestBody XlmOrderverify xlmOrderverify) {
        try {
            xlmOrderverifyService.submitmessage(xlmOrderverify);
            return R.ok("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("提交失败");
        }
    }

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param xlmOrderverify 订单提交记录
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('demo_xlmorderverify_get')")
    public R getXlmOrderverifyPage(Page page, XlmOrderverify xlmOrderverify) {
        return R.ok(xlmOrderverifyService.page(page, Wrappers.query(xlmOrderverify)));
    }


    /**
     * 通过id查询订单提交记录
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
//    @PreAuthorize("@pms.hasPermission('demo_xlmorderverify_get')" )
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(xlmOrderverifyService.getById(id));
    }

    /**
     * 新增订单提交记录
     *
     * @param xlmOrderverify 订单提交记录
     * @return R
     */
    @ApiOperation(value = "新增订单提交记录", notes = "新增订单提交记录")
    @SysLog("新增订单提交记录")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('demo_xlmorderverify_add')")
    public R save(@RequestBody XlmOrderverify xlmOrderverify) {
        return R.ok(xlmOrderverifyService.save(xlmOrderverify));
    }

    /**
     * 修改订单提交记录
     *
     * @param xlmOrderverify 订单提交记录
     * @return R
     */
    @ApiOperation(value = "修改订单提交记录", notes = "修改订单提交记录")
    @SysLog("修改订单提交记录")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('demo_xlmorderverify_edit')")
    public R updateById(@RequestBody XlmOrderverify xlmOrderverify) {
        return R.ok(xlmOrderverifyService.updateById(xlmOrderverify));
    }

    /**
     * 通过id删除订单提交记录
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除订单提交记录", notes = "通过id删除订单提交记录")
    @SysLog("通过id删除订单提交记录")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('demo_xlmorderverify_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(xlmOrderverifyService.removeById(id));
    }


}
